<?php
/* 
 *销售小订单详情
 */
class Order_detailAction extends TransDetailAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $map['name'] = array('like',"%".$key."%");//产品名称
            $map['mano'] = array('like',"%".$key."%");//销售单号
            $map['cas'] = array('like',"%".$key."%");//
            $map['_logic'] = "or";
        }
        if(!empty($_REQUEST['order_id'])){
           $map['order_id'] = $_REQUEST['order_id'];
        }
        if(!empty($_REQUEST['user_id'])){
           $map['user_id'] = $_REQUEST['user_id'];
        }
        //搜索发货单下的订单所在剩余小订单
        if(!empty($_REQUEST['sid'])){
            $rs = M('Shipping')->where('id = '. $_REQUEST['sid'])->find();
            $order_id = $rs['order_id'];
            $order_detail_ids = M('Shipping_detail')->where('shipping_id = '.$_REQUEST['sid'])->field('id')->findAll();
            $data = array();
            foreach ($order_detail_ids as $key => $value) {
                $data[$key] = $value['id'];
            }
            $oids = implode(',', $data);
           $map['order_id'] = $order_id;
           $map['Order_detail.id'] = array('not in'=>$oids);
        }
    }

    public function _before_index(){
        parent::_before_index();
        $this->assign('customers',M('Customer')->where('status = 1')->field('id,name')->findAll());
        $this->assign('users',M('User')->where('status = 1')->field('id,name')->findAll());
    }
    public function  index() {
        parent::index('OrderView');    
    }

    public function _before_edit()
    {
        $msgs = M('Message')->where('obj_type="Order_detail" and obj_id ='.$_REQUEST['id'])->order('create_time desc')->findAll();
        $this->assign('msgs',$msgs);
    }

    public function full_edit(){
    	$this->edit();
    }
    
    public function update(){
        if(isset($_POST['price']) && isset($_POST['number']) ){
            $_POST['total'] = $_POST['price'] * $_POST['number'];
            if(isset($_POST['logifee']))
                $_POST['should_pay'] = $_POST['price'] * $_POST['number'] + $_POST['logifee']; //应付款
        }
        if(isset($_POST['should_pay']) && isset($_POST['to_first_pay']) )   
            $_POST['to_remain_pay'] = $_POST['should_pay'] - $_POST['to_first_pay']; //余款= 应付款-预付款
        $_POST['delivery_date'] = empty($_POST['delivery_date'] )? 0 : strtotime($_POST['delivery_date']); 
        parent::update();
    }

     //目前暂用于批量分配负责人
    public function mass_update()
    {
         $data = $_POST;
        $count_no = count($data['charge_man']);
        for($i =0; $i < $count_no;$i++) {
          $_POST['id'] =  $data['id'][$i];
        //批量指定负责人
        if(!empty($data['charge_man'][$i]))
            $_POST['charge_man'] = $data['charge_man'][$i];
        
            $this->micro_update($_POST['id']);

          unset($_POST);
        }

        $jumpUrl = $_REQUEST['jumpUrl'];
        if(empty($jumpUrl))
            $jumpUrl = Cookie::get ( '_currentUrl_' );
        $this->assign ( 'jumpUrl', $jumpUrl );
        $this->success ('批量修改成功!');
    }

    public function cancel_order(){
        $id = trim($_REQUEST['id']);
        $new_id = M('Order_detail')->where('id = '.$id)->setField('status',-2);
        if( $new_id )
            $this->success("订单取消成功！");
        else
            $this->error("订单取消失败，或已经取消");
    }
    //客户催单
    public function urge_order(){
        $id = trim($_REQUEST['id']);
        $new_id = M('Order_detail')->setInc('priority','id = '.$id,4);
        if( $new_id ){
            //对应采购订单的优先级也跟着调(已经作废的供应商采购单除外)
            M('Buy')->where('status != 10')->setInc('priority','order_detail_id = '.$id,4);
            $this->success("客户催单成功！");
        }
        else
            $this->error("客户催单失败，或已经催单！");
    }

}
?>
